package com.echoesnet.echoesshortvideo.Repositories;

import com.echoesnet.echoesshortvideo.Api.HttpMethods;
import com.echoesnet.echoesshortvideo.Api.RequestHeader;
import com.echoesnet.echoesshortvideo.AppController;
import com.echoesnet.echoesshortvideo.Beans.VideoDetailEntity;
import com.echoesnet.echoesshortvideo.Constant.Api;
import com.echoesnet.echoesshortvideo.Persistence.AppDatabase;
import com.echoesnet.echoesshortvideo.Persistence.VideoDetailDao;
import com.google.gson.reflect.TypeToken;

import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.functions.Function;

/**
 * Copyright (C) 2018 科技发展有限公司
 * 完全享有此软件的著作权，违者必究
 * 负责数据处理
 *
 * @author ben
 * @version 1.0
 * @modifier
 * @createDate 2018/4/19 18:12
 * @description
 */

public class VideoDetailRepository
{
    private final VideoDetailDao userDao;

    public VideoDetailRepository()
    {
        this.userDao = AppDatabase.getInstance(AppController.getInstance()).videoDetailDao();
    }

    public void getVideoDetail(String videoId, String openSource, String toUserId, String getDataDirection, Observer<VideoDetailEntity> observer)
    {
        Map<String, Object> reqMap = RequestHeader.getCommonPartOfParam();
        reqMap.put(RequestHeader.VIDEO_SOURCE, openSource);
        reqMap.put(RequestHeader.OTHER_USER_ID, toUserId);
        reqMap.put(RequestHeader.VIDEO_ID, videoId);
        reqMap.put(RequestHeader.VIDEO_DATA_DIRECTION, getDataDirection);
        HttpMethods.getInstance().startServerRequest(observer, new Function<String, VideoDetailEntity>()
        {
            @Override
            public VideoDetailEntity apply(String response) throws Exception
            {
                VideoDetailEntity videoDetailEntity = AppController.getInstance().getGsonInstance().fromJson(response,
                        new TypeToken<VideoDetailEntity>()
                        {
                        }.getType());
                return videoDetailEntity;
            }
        }, Api.VIDEOC_DETAIL, reqMap);

        //return userDao.loadVideosByIds();
    }

    public void getMoreVideoDetail(String videoId, String openSource, String toUserId, String getDataDirection, Observer<VideoDetailEntity> observer)
    {
        Map<String, Object> reqMap = RequestHeader.getCommonPartOfParam();
        reqMap.put(RequestHeader.VIDEO_SOURCE, openSource);
        reqMap.put(RequestHeader.OTHER_USER_ID, toUserId);
        reqMap.put(RequestHeader.VIDEO_ID, videoId);
        reqMap.put(RequestHeader.VIDEO_DATA_DIRECTION, getDataDirection);
        HttpMethods.getInstance().startServerRequest(observer, new Function<String, VideoDetailEntity>()
        {
            @Override
            public VideoDetailEntity apply(String response) throws Exception
            {
                VideoDetailEntity videoDetailEntity = AppController.getInstance().getGsonInstance().fromJson(response,
                        new TypeToken<VideoDetailEntity>()
                        {
                        }.getType());
                return videoDetailEntity;
            }
        }, Api.VIDEOC_DETAIL, reqMap);

        //return userDao.loadVideosByIds();
    }

    public void likeVideo(String interfaceName, String videoId, Observer<String> observer)
    {
        Map<String, Object> reqMap = RequestHeader.getCommonPartOfParam();
        reqMap.put(RequestHeader.VIDEO_ID, videoId);
        HttpMethods.getInstance().startServerRequest(observer, interfaceName, reqMap, true);
    }
    public void commentVideo(String interfaceName, String videoId, String commentContent, Observer<String> observer)
    {
        Map<String, Object> reqMap = RequestHeader.getCommonPartOfParam();
        reqMap.put(RequestHeader.VIDEO_ID, videoId);
        reqMap.put(RequestHeader.COMMENT, commentContent);
        HttpMethods.getInstance().startServerRequest(observer, interfaceName, reqMap, true);
    }

    public void shareVideo(String interfaceName, String videoId, Observer<String> observer)
    {
        Map<String, Object> reqMap = RequestHeader.getCommonPartOfParam();
        reqMap.put(RequestHeader.VIDEO_ID, videoId);
        HttpMethods.getInstance().startServerRequest(observer, interfaceName, reqMap, true);
    }

    public void focus(String interfaceName, String toUserId, Observer<String> observer)
    {
        Map<String, Object> reqMap = RequestHeader.getCommonPartOfParam();
        reqMap.put(RequestHeader.OTHER_USER_ID, toUserId);
        HttpMethods.getInstance().startServerRequest(observer, interfaceName, reqMap, true);
    }

    public void deleteVideo(String interfaceName, String videoId, Observer<String> observer)
    {
        Map<String, Object> reqMap = RequestHeader.getCommonPartOfParam();
        reqMap.put(RequestHeader.VIDEO_ID, videoId);
        HttpMethods.getInstance().startServerRequest(observer, interfaceName, reqMap, true);
    }

}
